#include <stdio.h>
#include <string.h>

struct sf
{
	char x,y,z;
	unsigned char step;
}queue[51*51*51];

char sign[51][51][51];
int n;
char map[51][51];
int head,tail;
int find;


main()
{
	int i,j;	
	int x,y,z,step;
	char s[3];
	while (1)
	{
		scanf("%d", &n);
		if (n==0) break;
		scanf("%d%d%d", &queue[1].x, &queue[1].y, &queue[1].z);
		for (i=1;i<=n;i++)
			for (j=1;j<=n;j++)
			{
				scanf("%s", s);
				map[i][j]=s[0];
			}

		queue[1].step=0;
		memset(sign,0,sizeof(sign));
		sign[queue[1].x][queue[1].y][queue[1].z]=1;
		head=tail=1;
		find=0;
	
			

	while (head<=tail)
	{
		x=queue[head].x;
		y=queue[head].y;
		z=queue[head].z;
		if (x==y && y==z)
		{
			find=1;
			break;
		}
		step=queue[head].step;
		for (i=1;i<=n;i++)
		{
			if (map[x][i]==map[y][z] && sign[i][y][z]==0)
			{
				tail++;
				//queue[tail].x=i;
				//queue[tail].y=y;
				//queue[tail].z=z;
				//queue[tail].step=step+1;
				queue[tail]=queue[head];
				queue[tail].step++;
				queue[tail].x=i;
				sign[i][y][z]=1;

			}
			if (map[y][i]==map[x][z] && sign[x][i][z]==0)
			{
				tail++;

				//queue[tail].x=x;
				//queue[tail].y=i;
				//queue[tail].z=z;
				queue[tail]=queue[head];
				queue[tail].step++;
				queue[tail].y=i;
				sign[x][i][z]=1;
			}
			if (map[z][i]==map[x][y] && sign[x][y][i]==0)
			{
				tail++;
				//queue[tail].x=x;
				//queue[tail].y=y;
				//queue[tail].z=i;
				queue[tail]=queue[head];
				queue[tail].step++;
				queue[tail].z=i;
				sign[x][y][i]=1;
			}

		}
		head++;
	}
		if (find) printf("%d\n",queue[head].step);
		else printf("impossible\n");
	}

	return 0;
}